package calao.client.ui;

import java.util.Iterator;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

class CheckList 
extends VerticalPanel
implements NumberedListBox.ListWidget, ClickListener
{
		
	class Item extends CheckBox {
		final String value;
		Item(String text, String value) {
			super(text);
			this.value = value;
			this.addClickListener(CheckList.this);
		}
	}
	
	final NumberedListBox parent;
	
	CheckList(NumberedListBox parent)
	{
		this.parent = parent;
	}
	
	public void addItem(String text, String value)
	{
		add(new Item(text, value));
	}
	
	public void setItemSelected(int index, boolean selected)
	{
		((Item)getWidget(index)).setChecked(selected);
	}
	
	public int getItemCount()
	{
		return getWidgetCount();
	}
	
	public String getValue(int index)
	{
		return ((Item)getWidget(index)).value;
	}
	
	public String getItemText(int index)
	{
		return ((Item)getWidget(index)).getText();
	}

	public boolean isItemSelected(int index)
	{
		return ((Item)getWidget(index)).isChecked();		
	}

	public void onClick(Widget sender)
	{
		parent.listeners.fireChange(parent);
	}
	
	public void setEnabled(boolean b)
	{
		Iterator iter = getChildren().iterator();
		while (iter.hasNext()) {
			((Item)iter.next()).setEnabled(b);
		}
	}

}
